
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>插入排序 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="选择排序.html" />
    
    
    <link rel="prev" href="内部排序.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    数据结构（C语言版） 复习总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1.html">
            
                <a href="../chapter1.html">
            
                    
                    第一章 绪论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/chapter2.html">
            
                <a href="../chapter2/chapter2.html">
            
                    
                    第二章 线性表
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/顺序表.html">
            
                <a href="../chapter2/顺序表.html">
            
                    
                    线性表的顺序表示和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/链表.html">
            
                <a href="../chapter2/链表.html">
            
                    
                    线性表的链式表示和实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/chapter3.html">
            
                <a href="../chapter3/chapter3.html">
            
                    
                    第三章 栈和队列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/栈.html">
            
                <a href="../chapter3/栈.html">
            
                    
                    抽象数据类型栈的定义和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/队列.html">
            
                <a href="../chapter3/队列.html">
            
                    
                    抽象数据类型队列的定义和实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    第四章 串
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/串类型.html">
            
                <a href="../chapter4/串类型.html">
            
                    
                    串类型的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/串的表示和实现.html">
            
                <a href="../chapter4/串的表示和实现.html">
            
                    
                    串的表示和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter4/串的模式匹配算法.html">
            
                <a href="../chapter4/串的模式匹配算法.html">
            
                    
                    串的模式匹配算法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    第五章 数组和广义表
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/数组的定义.html">
            
                <a href="../chapter5/数组的定义.html">
            
                    
                    数组的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/矩阵的压缩存储.html">
            
                <a href="../chapter5/矩阵的压缩存储.html">
            
                    
                    矩阵的压缩存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter5/广义表.html">
            
                <a href="../chapter5/广义表.html">
            
                    
                    广义表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    第六章 树和二叉树
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter6/树.html">
            
                <a href="../chapter6/树.html">
            
                    
                    树的定义与基本术语
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/二叉树.html">
            
                <a href="../chapter6/二叉树.html">
            
                    
                    二叉树
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.2.1" data-path="../chapter6/二叉树的二叉链表实现.html">
            
                <a href="../chapter6/二叉树的二叉链表实现.html">
            
                    
                    二叉树的二叉链表实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter6/遍历二叉树和线索二叉树.html">
            
                <a href="../chapter6/遍历二叉树和线索二叉树.html">
            
                    
                    遍历二叉树和线索二叉树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter6/树和森林.html">
            
                <a href="../chapter6/树和森林.html">
            
                    
                    树和森林
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter6/赫夫曼树.html">
            
                <a href="../chapter6/赫夫曼树.html">
            
                    
                    赫夫曼树
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    第七章 图
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter7/图.html">
            
                <a href="../chapter7/图.html">
            
                    
                    图的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter7/图的存储结构.html">
            
                <a href="../chapter7/图的存储结构.html">
            
                    
                    图的存储结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter7/图的遍历.html">
            
                <a href="../chapter7/图的遍历.html">
            
                    
                    图的遍历
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter7/最小生成树.html">
            
                <a href="../chapter7/最小生成树.html">
            
                    
                    最小生成树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter7/有向无环图.html">
            
                <a href="../chapter7/有向无环图.html">
            
                    
                    有向无环图的应用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter8/查找.html">
            
                <a href="../chapter8/查找.html">
            
                    
                    第八章 查找
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter8/静态查找表.html">
            
                <a href="../chapter8/静态查找表.html">
            
                    
                    静态查找表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter8/动态查找表.html">
            
                <a href="../chapter8/动态查找表.html">
            
                    
                    动态查找表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter8/哈希表.html">
            
                <a href="../chapter8/哈希表.html">
            
                    
                    哈希表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="内部排序.html">
            
                <a href="内部排序.html">
            
                    
                    第九章 内部排序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.10.1" data-path="插入排序.html">
            
                <a href="插入排序.html">
            
                    
                    插入排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="选择排序.html">
            
                <a href="选择排序.html">
            
                    
                    选择排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="快速排序.html">
            
                <a href="快速排序.html">
            
                    
                    快速排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="归并排序和基数排序.html">
            
                <a href="归并排序和基数排序.html">
            
                    
                    归并排序和基数排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="排序方法的比较.html">
            
                <a href="排序方法的比较.html">
            
                    
                    排序方法的比较
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >插入排序</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x63D2;&#x5165;&#x6392;&#x5E8F;">&#x63D2;&#x5165;&#x6392;&#x5E8F;</h2>
<h3 id="&#x76F4;&#x63A5;&#x63D2;&#x5165;&#x6392;&#x5E8F;">&#x76F4;&#x63A5;&#x63D2;&#x5165;&#x6392;&#x5E8F;</h3>
<p><strong>&#x76F4;&#x63A5;&#x63D2;&#x5165;&#x6392;&#x5E8F;</strong>&#x8FDB;&#x672C;&#x64CD;&#x4F5C;&#x662F;&#x5C06;&#x4E00;&#x4E2A;&#x8BB0;&#x5F55;&#x63D2;&#x5165;&#x5230;&#x5DF2;&#x7ECF;&#x6392;&#x5E8F;&#x597D;&#x7684;&#x6709;&#x5E8F;&#x8868;&#x4E2D;&#xFF0C;&#x4ECE;&#x800C;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#xFF0C;&#x8BB0;&#x5F55;&#x6570;&#x589E; 1 &#x7684;&#x6709;&#x5E8F;&#x8868;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x5728;&#x67E5;&#x627E;&#x63D2;&#x5165;&#x4F4D;&#x7F6E;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x907F;&#x514D;&#x6570;&#x7EC4;&#x4E0B;&#x6807;&#x51FA;&#x754C;&#xFF0C;&#x5728; <code>R[0]</code> &#x5904;&#x8BBE;&#x7F6E;&#x76D1;&#x89C6;&#x54E8;&#x3002;&#x5728;&#x81EA; i-1 &#x8D77;&#x5F80;&#x524D;&#x641C;&#x7D22;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x540E;&#x79FB;&#x8BB0;&#x5F55;&#x3002;&#x6574;&#x4E2A;&#x6392;&#x5E8F;&#x8FC7;&#x7A0B;&#x4E3A;&#x8FDB;&#x884C; n-1 &#x8D9F;&#x63D2;&#x5165;&#x3002;&#x76F4;&#x63A5;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x7684;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x4E3A; <em>O</em>(<em>n<sup>2</sup></em>)&#x3002;</p>
<pre><code class="lang-C"><span class="hljs-comment">/**
 * &#x5BF9;&#x987A;&#x5E8F;&#x8868; L &#x8FDB;&#x884C;&#x63D2;&#x5165;&#x6392;&#x5E8F;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">InsertSort</span><span class="hljs-params">(SqList &amp;L)</span>
</span>{
    <span class="hljs-keyword">int</span> i, j;
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">2</span>; i &lt;= L.length; i++)
    {
        <span class="hljs-keyword">if</span> (L.data[i].key &lt; L.data[i<span class="hljs-number">-1</span>].key)
        {
            L.data[<span class="hljs-number">0</span>] = L.data[i];
            L.data[i] = L.data[i<span class="hljs-number">-1</span>];
            <span class="hljs-keyword">for</span>(j = i<span class="hljs-number">-2</span>; L.data[<span class="hljs-number">0</span>].key &lt; L.data[j]; j++)
            {
                L.data[j+<span class="hljs-number">1</span>] = L.data[j];
            }
            L.data[j+<span class="hljs-number">1</span>] = L.data[<span class="hljs-number">0</span>];
        }
    }
}
</code></pre>
<p>&#x5728;&#x51CF;&#x5C11;&#x201C;&#x6BD4;&#x8F83;&#x201D;&#x548C;&#x201C;&#x79FB;&#x52A8;&#x201D;&#x8FD9;&#x4E24;&#x79CD;&#x64CD;&#x4F5C;&#x7684;&#x6B21;&#x6570;&#x7740;&#x773C;&#xFF0C;&#x6709;&#x4EE5;&#x4E0B;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x65B9;&#x6CD5;&#xFF1A;</p>
<p><strong>1. &#x6298;&#x534A;&#x63D2;&#x5165;&#x6392;&#x5E8F;</strong></p>
<p>&#x67E5;&#x627E;&#x63D2;&#x5165;&#x4F4D;&#x7F6E;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F7F;&#x7528;&#x6298;&#x534A;&#x67E5;&#x627E;&#x3002;&#x6298;&#x534A;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x4EC5;&#x51CF;&#x5C11;&#x4E86;&#x5173;&#x952E;&#x5B57;&#x4E4B;&#x95F4;&#x7684;&#x6BD4;&#x8F83;&#x6B21;&#x5E8F;&#xFF0C;&#x4F46;&#x662F;&#x8BB0;&#x5F55;&#x7684;&#x79FB;&#x52A8;&#x6B21;&#x6570;&#x4E0D;&#x53D8;&#xFF0C;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x4ECD;&#x4E3A; <em>O</em>(<em>n<sup>2</sup></em>)&#x3002;</p>
<p><strong>2. 2-&#x8DEF;&#x63D2;&#x5165;&#x6392;&#x5E8F;</strong></p>
<p>2-&#x8DEF;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x662F;&#x5728;&#x6298;&#x534A;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x7684;&#x4E00;&#x79CD;&#x6539;&#x8FDB;&#xFF0C;&#x5176;&#x76EE;&#x7684;&#x662F;&#x51CF;&#x5C11;&#x6392;&#x5E8F;&#x8FC7;&#x7A0B;&#x4E2D;&#x79FB;&#x52A8;&#x8BB0;&#x5F55;&#x7684;&#x6B21;&#x6570;&#xFF0C;&#x4F46;&#x4E3A;&#x6B64;&#x9700;&#x8981; n &#x4E2A;&#x8BB0;&#x5F55;&#x7684;&#x8F85;&#x52A9;&#x7A7A;&#x95F4;&#x3002;</p>
<p>&#x53E6;&#x8BBE;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#x6570;&#x7EC4; <code>d[]</code>&#xFF0C;&#x9996;&#x5148;&#x5C06; <code>L.data[1]</code> &#x8D4B;&#x503C;&#x7ED9; <code>d[1]</code>&#xFF0C;&#x7136;&#x540E;&#x4EE5; <code>d[1]</code> &#x5185;&#x5BB9;&#x4E3A;&#x4E2D;&#x503C;&#xFF0C;&#x5C06; <code>L.data[i]</code> &#x5143;&#x7D20;&#x9010;&#x4E2A;&#x63D2;&#x5165;&#x5230; <code>d[1]</code> &#x503C;&#x4E4B;&#x524D;&#x6216;&#x4E4B;&#x540E;&#x7684;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x4E2D;&#x3002;</p>
<p><strong>3. &#x8868;&#x63D2;&#x5165;&#x6392;&#x5E8F;</strong></p>
<p>&#x8868;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;&#x4EE5;&#x9759;&#x6001;&#x94FE;&#x8868;&#x7C7B;&#x578B;&#x4F5C;&#x4E3A;&#x5F85;&#x6392;&#x5E8F;&#x8BB0;&#x5F55;&#x7684;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#x3002;&#x8BBE;&#x6570;&#x7EC4;&#x4E2D;&#x4E0B;&#x6807;&#x4E3A; 0 &#x7684;&#x5206;&#x91CF;&#x4E3A;&#x8868;&#x5934;&#x7ED3;&#x70B9;&#xFF0C;&#x5E76;&#x4EE4;&#x8868;&#x5934;&#x7ED3;&#x70B9;&#x8BB0;&#x5F55;&#x7684;&#x5173;&#x952E;&#x5B57;&#x53D6;&#x6700;&#x5927;&#x6574;&#x6570; MAXINT&#x3002;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x4ECD;&#x4E3A; <em>O</em>(<em>n<sup>2</sup></em>)&#x3002;</p>
<pre><code class="lang-C"><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> SIZE 100 <span class="hljs-comment">// &#x9759;&#x6001;&#x94FE;&#x8868;&#x5BB9;&#x91CF;</span></span>

<span class="hljs-comment">/**
 * &#x9759;&#x6001;&#x94FE;&#x8868;&#x7ED3;&#x70B9;&#x7ED3;&#x6784;
 */</span>
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span>
{
    ElemType rc; <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x9879;</span>
    <span class="hljs-keyword">int</span> next; <span class="hljs-comment">// &#x4E0B;&#x4E00;&#x5143;&#x7D20;&#x6307;&#x9488;&#x9879;</span>
}SLNode;

<span class="hljs-comment">/**
 * &#x8868;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x9759;&#x6001;&#x94FE;&#x8868;
 */</span>
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span>
{
    SLNode data[size]; <span class="hljs-comment">// 0 &#x53F7;&#x5355;&#x5143;&#x4E3A;&#x8868;&#x5934;&#x7ED3;&#x70B9;</span>
    <span class="hljs-keyword">int</span> length; <span class="hljs-comment">// &#x94FE;&#x8868;&#x5F53;&#x524D;&#x957F;&#x5EA6;</span>
}SLinkListType;
</code></pre>
<p>&#x8868;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x7684;&#x7ED3;&#x679C;&#x53EA;&#x662F;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x6709;&#x5E8F;&#x94FE;&#x8868;&#xFF0C;&#x67E5;&#x627E;&#x8BB0;&#x5F55;&#x65F6;&#x53EA;&#x80FD;&#x6EE1;&#x8DB3;&#x987A;&#x5E8F;&#x67E5;&#x627E;&#x65B9;&#x5F0F;&#xFF0C;&#x4E3A;&#x4E86;&#x5B9E;&#x73B0;&#x6709;&#x5E8F;&#x8868;&#x7684;&#x6298;&#x534A;&#x67E5;&#x627E;&#xFF0C;&#x9700;&#x8981;&#x5BF9;&#x8BB0;&#x5F55;&#x8FDB;&#x884C;<strong>&#x91CD;&#x65B0;&#x6392;&#x5E8F;</strong>&#x3002;&#x91CD;&#x6392;&#x8BB0;&#x5F55;&#x7684;&#x505A;&#x6CD5;&#xFF1A;&#x987A;&#x5E8F;&#x626B;&#x63CF;&#x9759;&#x6001;&#x94FE;&#x8868;&#xFF0C;&#x5C06;&#x94FE;&#x8868;&#x4E2D;&#x7B2C; <em>i</em> &#x4E2A;&#x7ED3;&#x70B9;&#x79FB;&#x52A8;&#x5230;&#x6570;&#x7EC4;&#x7684;&#x7B2C; <em>i</em> &#x4E2A;&#x5206;&#x91CF;&#x4E2D;&#x3002;&#xFF08;&#x8BE6;&#x89C1;&#x53C2;&#x8003;&#x8D44;&#x6599; 268 &#x9875;&#xFF09;</p>
<h3 id="&#x5E0C;&#x5C14;&#x6392;&#x5E8F;">&#x5E0C;&#x5C14;&#x6392;&#x5E8F;</h3>
<p>&#x5E0C;&#x5C14;&#x6392;&#x5E8F;&#xFF08;Shell&apos;s Sort&#xFF09;&#x53C8;&#x79F0;&#x7F29;&#x5C0F;&#x589E;&#x91CF;&#x6392;&#x5E8F;&#xFF08;&#x5C5E;&#x4E8E;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#xFF09;&#xFF0C;&#x5B83;&#x7684;&#x57FA;&#x672C;&#x601D;&#x60F3;&#x662F;&#xFF1A;&#x5148;&#x5C06;&#x6574;&#x4E2A;&#x5F85;&#x6392;&#x8BB0;&#x5F55;&#x5E8F;&#x5217;&#x5206;&#x5272;&#x6210;&#x82E5;&#x5E72;&#x5B50;&#x5E8F;&#x5217;&#x5206;&#x522B;&#x8FDB;&#x884C;&#x76F4;&#x63A5;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#xFF0C;&#x5F85;&#x6574;&#x4E2A;&#x5E8F;&#x5217;&#x4E2D;&#x7684;&#x8BB0;&#x5F55;&#x201C;&#x57FA;&#x672C;&#x6709;&#x5E8F;&#x201D;&#x65F6;&#xFF0C;&#x518D;&#x5BF9;&#x5168;&#x4F53;&#x8BB0;&#x5F55;&#x8FDB;&#x884C;&#x4E00;&#x6B21;&#x76F4;&#x63A5;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x3002;&#xFF08;&#x8BE6;&#x7EC6;&#x7684;&#x6392;&#x5E8F;&#x8FC7;&#x7A0B;&#x89C1;&#x53C2;&#x8003;&#x8D44;&#x6599; 271 &#x9875;&#xFF09;</p>
<p>&#x5E0C;&#x5C14;&#x6392;&#x5E8F;&#x7684;&#x7279;&#x70B9;&#xFF1A;&#x5B50;&#x5E8F;&#x5217;&#x7684;&#x6784;&#x6210;&#x4E0D;&#x662F;&#x7B80;&#x5355;&#x5730;&#x201C;&#x9010;&#x6BB5;&#x5206;&#x5272;&#x201D;&#xFF0C;&#x800C;&#x662F;&#x5C06;&#x76F8;&#x9694;&#x67D0;&#x4E2A;&#x201C;&#x589E;&#x91CF;&#x201D;&#x7684;&#x51E0;&#x4E2A;&#x8BB0;&#x5F55;&#x7EC4;&#x6210;&#x4E00;&#x4E2A;&#x5B50;&#x5E8F;&#x5217;&#xFF0C;&#x6BCF;&#x8D9F;&#x7684;&#x201C;&#x589E;&#x91CF;&#x201D;&#x9010;&#x8D9F;&#x51CF;&#x5C0F;&#xFF08;&#x4F8B;&#x5982;&#x4F9D;&#x6B21;&#x53D6;5&#x3001;3&#x3001;1&#xFF09;&#xFF0C;&#x76F4;&#x5230;&#x589E;&#x91CF;&#x4E3A; 1 &#x65F6;&#x4E3A;&#x6B62;&#x3002;</p>
<pre><code class="lang-C"><span class="hljs-comment">/**
 * &#x5E0C;&#x5C14;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;&#xFF08;&#x5347;&#x5E8F;&#xFF09;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">ShellSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> r[], <span class="hljs-keyword">int</span> n)</span>
</span>{
    <span class="hljs-keyword">int</span> i, j, d; <span class="hljs-comment">// d &#x4E3A;&#x589E;&#x91CF;</span>
    <span class="hljs-keyword">for</span>(d = n/<span class="hljs-number">2</span>; d &gt;= <span class="hljs-number">1</span>; d = d/<span class="hljs-number">2</span>)
    {
        <span class="hljs-keyword">for</span>(i = d+<span class="hljs-number">1</span>; i &lt;= n; i++)
        {
            r[<span class="hljs-number">0</span>] = r[i];
            <span class="hljs-keyword">for</span>(j = i-d; j &gt; <span class="hljs-number">0</span> &amp;&amp; r[<span class="hljs-number">0</span>] &lt; r[j]; j = j-d) <span class="hljs-comment">// &#x964D;&#x5E8F;&#x65F6;&#x66F4;&#x6539;&#x4E3A; r[0] &gt; r[j]</span>
            {
                r[j+d] = r[j];
            }
            r[j+d] = r[<span class="hljs-number">0</span>];
        }
    }
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="内部排序.html" class="navigation navigation-prev " aria-label="Previous page: 第九章 内部排序">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="选择排序.html" class="navigation navigation-next " aria-label="Next page: 选择排序">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"插入排序","level":"1.10.1","depth":2,"next":{"title":"选择排序","level":"1.10.2","depth":2,"path":"chapter9/选择排序.md","ref":"chapter9/选择排序.md","articles":[]},"previous":{"title":"第九章 内部排序","level":"1.10","depth":1,"path":"chapter9/内部排序.md","ref":"chapter9/内部排序.md","articles":[{"title":"插入排序","level":"1.10.1","depth":2,"path":"chapter9/插入排序.md","ref":"chapter9/插入排序.md","articles":[]},{"title":"选择排序","level":"1.10.2","depth":2,"path":"chapter9/选择排序.md","ref":"chapter9/选择排序.md","articles":[]},{"title":"快速排序","level":"1.10.3","depth":2,"path":"chapter9/快速排序.md","ref":"chapter9/快速排序.md","articles":[]},{"title":"归并排序和基数排序","level":"1.10.4","depth":2,"path":"chapter9/归并排序和基数排序.md","ref":"chapter9/归并排序和基数排序.md","articles":[]},{"title":"排序方法的比较","level":"1.10.5","depth":2,"path":"chapter9/排序方法的比较.md","ref":"chapter9/排序方法的比较.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter9/插入排序.md","mtime":"2018-08-30T01:35:54.528Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-02T10:05:33.540Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

